﻿@using Bddify.Core
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
		<link href="bddify.css" rel="stylesheet" />
		<title>Bddify Test Result @DateTime.Now.ToShortDateString()</title>
	</head>

	<body>
		<div id="main">
			<header>
				<div id="bddifyTitle">@Model.Configuration.ReportHeader</div>
				<div id="bddifyDescription">@Model.Configuration.ReportDescription</div>
			</header>

			<section class="summary">
        @{
          string assemblyName = string.Empty;
          if (Model.Stories.Count() > 0)
          {
            var scenario = Model.Stories.SelectMany(s => s.Scenarios).FirstOrDefault();
            if (scenario != null)
            {
              assemblyName = scenario.TestObject.GetType().Assembly.GetName().Name;
            }
          }
        }

        <h3>Assembly: '@assemblyName'</h3>
          
        @{ var scenarios = Model.Stories.SelectMany(s => s.Scenarios).ToList(); }

				<ul class="resultSummary">
					<li class="namespace">
            <div class="summary">
						  <div class="summaryLabel">Namespaces</div>
						  <span class="summaryCount">@Model.Stories.Where(b => b.MetaData == null).SelectMany(s => s.Scenarios).GroupBy(b => b.TestObject.GetType().Namespace).Count()</span>
            </div>
					</li>
					<li class="story">
            <div class="summary">
						  <div class="summaryLabel">Stories</div>
						  <span class="summaryCount">@Model.Stories.Where(b => b.MetaData != null).GroupBy(b => b.MetaData.Type).Count()</span>
            </div>
					</li>
					<li class="Passed">
            <div class="summary">
              <div class="summaryLabel">Passed</div>
						  <span class="summaryCount">@scenarios.Count(b => b.Result == StepExecutionResult.Passed)</span>
            </div>
					</li>
					<li class="Failed">
            <div class="summary">
						  <div class="summaryLabel">Failed</div>
						  <span class="summaryCount">@scenarios.Count(b => b.Result == StepExecutionResult.Failed)</span>
            </div>
					</li>
					<li class="Inconclusive">
            <div class="summary">
						  <div class="summaryLabel">Inconclusive</div>
						  <span class="summaryCount">@scenarios.Count(b => b.Result == StepExecutionResult.Inconclusive)</span>
            </div>
					</li>
					<li class="NotImplemented">
            <div class="summary">
						  <div class="summaryLabel">Not Implemented</div>
						  <span class="summaryCount">@scenarios.Count(b => b.Result == StepExecutionResult.NotImplemented)</span>
            </div>
  				</li>
					<li class="NotExecuted">
            <div class="summary">
						  <div class="summaryLabel">Not Executed</div>
						  <span class="summaryCount">@scenarios.Count(b => b.Result == StepExecutionResult.NotExecuted)</span>
            </div>
					</li>
				</ul>
      </section>

			<div id="testResult">
				<ul class="testResult">
          @{
            var groupedByNamespace = from story in Model.Stories
                                     where story.MetaData == null
                                     group story by story.Scenarios.First().TestObject.GetType().Namespace into g
                                     select g;

            var groupedByStories = from story in Model.Stories
                                   where story.MetaData != null
                                   group story by story.MetaData.Type.Name into g
                                   select g;

            var groupedScenarios = groupedByStories.Union(groupedByNamespace);

            foreach (var scenarioGroup in groupedScenarios)
            {
              var story = scenarioGroup.First();
              var scenariosInGroup = scenarioGroup.SelectMany(s => s.Scenarios);
              var storyResult = (StepExecutionResult)scenariosInGroup.Max(s => (int)s.Result); 
          <li>
            <div class="story @storyResult">
          	  <div class="storyMetaData">
              @if (story.MetaData == null)
              {
                <div class="namespaceName">@story.Scenarios.First().TestObject.GetType().Namespace</div>
              }
              else
              {
                <div class="storyTitle">@story.MetaData.Title</div>
              }

              @if (story.MetaData != null && !string.IsNullOrEmpty(story.MetaData.AsA))
              { 
                <ul class="storyNarrative">
                  <li>@story.MetaData.AsA </li>
                  <li>@story.MetaData.IWant </li>
                  <li>@story.MetaData.SoThat</li>
                </ul>
              }
              </div>

              <div class="scenarios">
                @foreach (var scenario in scenariosInGroup)
                { 
                <div class="scenario @scenario.Result" onclick="toggle('@scenario.Id');">
								  <div class="scenarioTitle">@scenario.ScenarioText</div>
                
							    <ul class="steps" id="@scenario.Id" style="display:none">
                  @foreach (var step in scenario.Steps.Where(s => s.ShouldReport))
                  {
                    string stepClass = string.Empty;
                    string result = step.StepTitle;
                    var reportException = step.Exception != null && step.Result == StepExecutionResult.Failed;
                    if (reportException)
                    {
                      stepClass = step.Result + "Exception";

                      if (!string.IsNullOrEmpty(step.Exception.Message))
                      {
                        result += " [Exception Message: '" + step.Exception.Message + "']";
                      }
                    }
                    <li class="step @step.Result @stepClass @step.ExecutionOrder" onclick="toggle('@step.Id');">
										  <span>@result</span>

                      @if (reportException)
                      {
									      <div class="step @stepClass" id="@step.Id">
										      <code>@step.Exception.StackTrace</code>
                        </div>
                      }

									  </li>
                  }
		  				    </ul>
						    </div>
                }
              </div>
						</div>
          </li>
              }
            }
				</ul>

        <p><span>Tested at: @DateTime.Now</span></p>
			</div>
		</div>

    <footer>Powered by <a href="https://code.google.com/p/bddify/">bddify</a> framework</footer>

		<script type="text/javascript">
		  function toggle(id) {
		    var e = document.getElementById(id);
		    if (e.style.display == 'none') {
		      e.style.display = '';
		    }
		    else {
		      e.style.display = 'none';
		    }
		  }
		</script>
	</body>
</html>